package sipus.action.master.helper;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.criterion.Restrictions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zkplus.hibernate.HibernateUtil;
import org.zkoss.zul.Borderlayout;
import org.zkoss.zul.Button;
import org.zkoss.zul.Center;
import org.zkoss.zul.Column;
import org.zkoss.zul.Columns;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Label;
import org.zkoss.zul.Panel;
import org.zkoss.zul.Panelchildren;
import org.zkoss.zul.Row;
import org.zkoss.zul.Rows;
import org.zkoss.zul.South;
import org.zkoss.zul.Timer;
import org.zkoss.zul.Toolbar;
import org.zkoss.zul.Window;

import sipus.common.Common;
import sipus.database.model.OnlineUsers;
import sipus.database.model.Tbmuser;


public class DaftarPenggunaOnline extends Window {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5620991583788581962L;

	private Toolbar toolbar;
	private Button batal;
	private String req;

	public DaftarPenggunaOnline(HttpServletRequest request) {
		super();
		req = request.getRequestURL().toString();
		req = req.replaceAll("http://", "");
		req = req.split(":")[0];
		req = req.split("/")[0];
		try {
			init();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	private void init() {
		Common.clear(this);
		setClosable(true);
		setTitle("Daftar Pengguna Online (Terdaftar)");
		setWidth("90%");
		setHeight("90%");
		setPosition("center");

		Panel panel = new Panel();
		panel.setParent(this);
		panel.setWidth("100%");
		panel.setHeight("100%");
		panel.setTitle("Daftar Pengguna Online yang sudah terdaftar");
		panel.setBorder("none");
		panel.setStyle("border:0px;");

		toolbar = new Toolbar();
		toolbar.setHeight("30px");
		toolbar.setParent(panel);
		batal = new Button("Refresh");

		batal.addEventListener("onClick", new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {
				init();
			}
		});
		batal.setParent(toolbar);

		Panelchildren panelchildren = new Panelchildren();
		panelchildren.setParent(panel);

		Borderlayout borderlayout = new Borderlayout();
		borderlayout.setParent(panelchildren);
		Center center = new Center();
		center.setParent(borderlayout);
		center.setFlex(true);

		Grid grid = new Grid();
		grid.setParent(center);
		grid.setWidth("100%");
		grid.setHeight("100%");
		// grid.setOddRowSclass("non-odd");

		Columns columns = new Columns();
		columns.setParent(grid);
		Column column = new Column("Nama Pengguna");
		column.setWidth("40%");
		column.setParent(columns);
		column = new Column("Jenis Pengguna");
		column.setWidth("15%");
		column.setParent(columns);

		column = new Column("Waktu Login");
		column.setWidth("25%");
		column.setParent(columns);
		column = new Column("Lama");
		column.setWidth("20%");
		column.setParent(columns);

		Rows rows = new Rows();
		rows.setParent(grid);

		List<OnlineUsers> onlineUsers = HibernateUtil.currentSession()
				.createCriteria(OnlineUsers.class)
				.add(Restrictions.eq("host", req)).list();
		for (OnlineUsers objects : onlineUsers) {

			Tbmuser tbmuser = objects.getTbmuser();
			String roleName = tbmuser == null || tbmuser.getUserRole() == null ? ""
					: tbmuser.getUserRole().getRoleName();
			if (roleName == null) {
				continue;
			}

			final Date jam = objects.getLogin().getLogin();

			Row row = new Row();
			row.setStyle("border:0px;background: transparent;");
			row.setParent(rows);
			row.appendChild(new Label(tbmuser.getUserNama()));
			row.appendChild(new Label(roleName));

			row.appendChild(new Label(jam == null ? "" : Common.dateFormat3
					.format(jam)));
			final Label time = new Label("0 detik 0 menit 0 jam");
			row.appendChild(time);

			Timer timer = new Timer(1000);
			timer.setRepeats(true);
			timer.setParent(row);
			timer.addEventListener("onTimer", new EventListener() {

				@Override
				public void onEvent(Event arg0) throws Exception {

					long diff = 0L;
					long diffDetik = 0L;
					long diffMenit = 0L;
					long diffJam = 0L;
					if (jam != null) {
						diff = new Date().getTime() - jam.getTime();
						diffDetik = (diff / (1000/* * 60 * 60 * 24 */)) % 60;
						diffMenit = (diff / (1000 * 60 /** 60 * 24 */
						)) % 60;
						diffJam = (diff / (1000 * 60 * 60 /** 24 */
						));
					}

					time.setValue(Common.numberFormat.format(diffJam) + " jam "
							+ Common.numberFormat.format(diffMenit) + " menit "
							+ Common.numberFormat.format(diffDetik) + " detik");

				}
			});
		}

		onlineUsers = null;
		// row = new Row();
		// row.setStyle("border:0px;background: transparent;");
		// row.setParent(rows);
		South south = new South();
		south.setFlex(true);
		south.setParent(borderlayout);

		toolbar = new Toolbar();
		toolbar.setHeight("30px");
		toolbar.setParent(south);
		batal = new Button("Tutup");

		batal.addEventListener("onClick", new EventListener() {
			@Override
			public void onEvent(Event event) throws Exception {
				DaftarPenggunaOnline.this.detach();
			}
		});
		batal.setParent(toolbar);

	}
}
